/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package soccer.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;
import java.util.Map;

import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.RepaintManager;
import javax.swing.plaf.TextUI;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import org.junit.Test;

import soccer.action.AdminActions;
import soccer.action.MatchActions;
import soccer.action.CoachActions;
import soccer.action.PlayerActions;
import soccer.action.StatisticActions;

import soccer.action.SystemActions;
import soccer.action.TeamActions;
import soccer.model.matches.Match;
import soccer.model.matches.MatchConstant;
import soccer.model.matchesDetail.MatchesDetail;
import soccer.model.player.Player;
import soccer.model.team.Team;
import soccer.model.team.TeamDAOImpl;
import soccer.view.AddCoachTeamPanel;
import soccer.view.AddPlayerTeamPanel;
import soccer.view.AddTeamMatchPanel;
import soccer.view.AddTeamPanel;
import soccer.view.AdminOptions;
import soccer.view.CoachSearchPanel;
import soccer.view.MainOptionPanel;
import soccer.view.MatchDetailPanel;
import soccer.view.MatchSearchPanel;
import soccer.view.PlayerSearchPanel;
import soccer.view.AdminCreatePanel;
import soccer.view.AdminLoginPanel;
import soccer.view.PlayerStatisticPanel;
import soccer.view.StatisticPanel;
import soccer.view.TeamSearchPanel;
import soccer.view.TeamStatisticPanel;
import soccer.view.UserOptionsPanel;
import soccer.view.ViewQA;
import sun.awt.RepaintArea;

/**
 *
 * @author pasa
 */
public class Controller {
    ViewQA vq = null;
    SystemActions systemActions = null;
    
    
    /**
     * @wbp.parser.entryPoint
     */
    public Controller(ViewQA vq, SystemActions systemActions){
      this.vq = vq;
      this.systemActions = systemActions;
      
      vq.getAddTeamPanel().addTeamActionListener(new addTeamAction());
      vq.getTeamAddedVerifyPanel().setAgainAddNewButtonListener(new addAgainNewTeamButtonAction());
      vq.getAddTeamPanel().addCoachBackToSystem(new selectBackToAdminOptions());
      vq.getAddTeamMatchPanel().addCoachBackToSystem(new selectBackToAdminOptions());
      vq.getAddCoachTeamPanel().addCoachBackToSystem(new selectBackToAdminOptions());
      vq.getAddPlayerTeamPanel().addCoachBackToSystem(new selectBackToAdminOptions());
      //vq.getAddTeamMatchPanel().addMatchButtonAction(new addTeamsMatchAction());
      //Coach
      vq.getCoachSearchPanel().addSearchByNameButtonActionListener(new searchCoachByNameAction());
      vq.getCoachSearchPanel().addSearchResultButtonActionListener(new searchCoachResultButtonAction());
      vq.getCoachSearchPanel().addSearchByTeamButtonActionListener(new searchCoachByNameAction());
      vq.getCoachAddedVerifyPanel().setsystemBackButtonListener(new systemBackButtonAction() );
      vq.getCoachAddedVerifyPanel().setAgainAddNewButtonListener(new addAgainNewCoach());
      vq.getAddCoachTeamPanel().addTeamComboboxItemListener(new addCoachTeamComboBoxAction());
      vq.getAddCoachTeamPanel().addCoachButtonActionListener(new addCoachAction());
      // Team Search Panel
      vq.getTeamSearchPanel().addSearchByCityRadioButtonActionListener(new searchRadioButtonAction());
      vq.getTeamSearchPanel().addSearchByNameRadioButtonActionListener(new searchRadioButtonAction());
      vq.getTeamSearchPanel().addSearchTeamButtonActionListener(new searchTeamButtonAction());
       
      // Add Player Panel
    
      vq.getAddPlayerTeamPanel().addTeamComboboxItemListener(new addPlayerTeamComboBoxAction());
      vq.getAddPlayerTeamPanel().addPlayerButtonActionListener(new addPlayerButtonAction());
      vq.getPlayerAddedVerifyPanel().setAgainAddNewButtonListener(new addAgainNewPlayerButtonAction());
      vq.getPlayerAddedVerifyPanel().setsystemBackButtonListener(new systemBackButtonAction());
      
      //search Player Panel
      PlayerSearchPanel playerSearchPanel = vq.getPlayerSearchPanel();
      playerSearchPanel.addSearchLicenceRadioButtonActionListener(new searchPlayerOptionRadioButtonAction());
      playerSearchPanel.addSearchNameRadioButtonActionListener(new searchPlayerOptionRadioButtonAction() );
      playerSearchPanel.addSearchTeamRadioButton(new searchPlayerOptionRadioButtonAction());
      playerSearchPanel.addSearchPlayerButtonActionListener(new searchPlayerButtonAction());
      
      //search Match Panel
      MatchSearchPanel matchSearchPanel = vq.getMatchSearchPanel();
      matchSearchPanel.addSearchByDateRadioButtonActionListener(new searchMatchOptionRadioButtonAction());
      matchSearchPanel.addSearchByTeamRadioButtonActionListener(new searchMatchOptionRadioButtonAction());
      matchSearchPanel.addSearchMatchButtonActionListener(new searchMatchButtonAction());
      matchSearchPanel.setGoMatchDetailButton(new goMatchDetailOfMatchButtonAction());
      
      //matcDetail Panel
      MatchDetailPanel matchDetailPanel = vq.getMatchDetailPanel();
      matchDetailPanel.addBtnBackActionListener(new matchDetailBackButtonAction());
      //add Player ve search Player deki team comboboxı dolduruan kısım
      fillAddPlayerPanelComboBox();
      //Admin Create
      vq.getAdminCreatePanel().addCreateButtonActionListener(new addAdminCreateButtonAction());
      
      //Admin Login
      vq.getAdminLoginPanel().addLoginButtonActionListener(new addAdminLoginButtonAction());
    
      //Main Panel
      vq.getMainOptionPanel().selectAdminAction(new selectAdminButtonAction());
      vq.getMainOptionPanel().selectUserAction(new selectUserButtonAction());
      vq.getAdminOptions().selectEnterTeam(new selectEnterTeam());
      vq.getAdminOptions().selectEnterMatch(new selectEnterMatch());
      vq.getAdminOptions().selectEnterPlayer(new selectEnterPlayer());
      vq.getAdminOptions().selectEnterCoach(new selectEnterCoach());
      vq.getAdminOptions().selectEnterMatchResult(new selectEnterMatchResult());
      
      vq.getUserOptionsPanel().addTeamOptionButtonActionListener(new addTeamAction());
    
      //userOption Panel
      UserOptionsPanel userOptionsPanel = vq.getUserOptionsPanel();
      userOptionsPanel.addTeamOptionButtonActionListener(new teamOptionButtonAction());
      userOptionsPanel.addPlayerOptionsButtonActionListener(new playerOptionButtonAction());
      userOptionsPanel.addCoachOptionButtonActionListener(new coachOptionButtonAction());
      userOptionsPanel.addMatchOptionButtonActionListener(new matchOptionButtonAction());
      
      //statistic Panel
      StatisticPanel statisticPanel = vq.getStatisticPanel();
      statisticPanel.fillComboBoxWithWeek(2);
      statisticPanel.addWeekComboboxItemListener(new weekCombobox());
      statisticPanel.addPlayerStatisticButtonActionListener(new enterPlayerStatisticButton());
      
      fillLeagueTable(1);
      
      //playerStatistic Panel
     
      PlayerStatisticPanel playerStatisticPanel = vq.getPlayerStatisticPanel();
      playerStatisticPanel.addGetPlayerStatisticButtonActionListener(new getPlayerStatisticButtonAction());
      playerStatisticPanel.addbackButtonActionListener(new backStatisticButton());
      
    }
    
    class addTeamAction implements ActionListener{
       public void actionPerformed(ActionEvent e) {
       AddTeamPanel ad = vq.getAddTeamPanel();
       String teamName = ad.getTeamNameText().getText();
       String teamCode = ad.getTeamCodeText().getText();
       String teamCity = ad.getTeamCityText().getText();
       String establishDate = ad.getTeamEstablishDateText().getText();
       int tc =0;
       try{
         tc = Integer.parseInt(teamCode);
       }catch(NumberFormatException ex){
         JOptionPane.showMessageDialog(null, "team code shuld be number");
       }
       TeamActions teamAction = systemActions.getTeamActions();
       int rows = teamAction.addNewTeam(tc, teamName, establishDate, teamCity);
       if(rows > 0){
    	  vq.getContentPane().setVisible(false);
    	  vq.getTeamAddedVerifyPanel().getOkeyMessageLabel().setText("The teams is added succesfully");
    	  vq.getTeamAddedVerifyPanel().setVisible(true);
    	  vq.setContentPane(vq.getTeamAddedVerifyPanel());
          }
        }
    }
    class addAgainNewPlayerButtonAction implements ActionListener{
         @Override
		public void actionPerformed(ActionEvent e) {
		   	vq.getContentPane().setVisible(false);
		   	vq.getAddPlayerTeamPanel().setVisible(true);
		   	vq.setContentPane(vq.getAddPlayerTeamPanel());
		}
    	
    }
    class addAgainNewTeamButtonAction implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			vq.getContentPane().setVisible(false);
			vq.getAddTeamPanel().setVisible(true);
			vq.setContentPane(vq.getAddTeamPanel());
           }
    }
    class addAgainNewCoach implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			vq.getContentPane().setVisible(false);
			vq.getAddCoachTeamPanel().setVisible(true);
			vq.setContentPane(vq.getAddCoachTeamPanel());
		}
    	
    }
   // search Team Radio Button
    class searchRadioButtonAction implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			JLabel jl = vq.getTeamSearchPanel().getSearchTeamLabel();
			if(e.getActionCommand().equals("searchCity")){
                jl.setText("Team City :");
			}else if(e.getActionCommand().equals("searchName")){
				jl.setText("Team Name :");
			}
		}
    }
    
    // search Button in team search panel
    class searchTeamButtonAction implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
          List teamList = null;
          TeamSearchPanel teamSearchPanel = vq.getTeamSearchPanel();
           String text = teamSearchPanel.getSearchTextField().getText();
           String errorMessage ;
           if(text == null || text.trim().length() < 1){
             errorMessage = "* text alanı boş olamaz";
             teamSearchPanel.showErrorMessage(errorMessage);
           return;
		   }
		if(teamSearchPanel.getSearchByCityRadioButton().isSelected()){
		  TeamActions teamActions = systemActions.getTeamActions();
		  teamList = teamActions.searchTeamByCity(text);
		}else if(teamSearchPanel.getSearchByNameRadioButton().isSelected()){
			TeamActions teamActions = systemActions.getTeamActions();
			teamList = teamActions.searchTeamByName(text);
		}
		if(teamList == null || teamList.size() < 1){
			teamSearchPanel.showErrorMessage("* The team can not be found. try again");
			return;
		}
		teamSearchPanel.addTeamsToTeamsTable(teamList);
		}
    }
    
    // add Player team panelde ki combobox ta degişiklik oldugunda çalısır
    class addPlayerTeamComboBoxAction implements ItemListener{

		@Override
		public void itemStateChanged(ItemEvent e) {
			// TODO Auto-generated method stub
			JComboBox cb = (JComboBox) e.getSource();
			String item = (String) cb.getSelectedItem();
			AddPlayerTeamPanel addPlayerTeamPanel = vq.getAddPlayerTeamPanel();
			if(item.equals("no team")){
				addPlayerTeamPanel.setPlayerAgrementPanelVisible(false);
			}else {
				addPlayerTeamPanel.setPlayerAgrementPanelVisible(true);
			}
		}
    	
    }
    class addCoachTeamComboBoxAction implements ItemListener{

		@Override
		public void itemStateChanged(ItemEvent e) {
			// TODO Auto-generated method stub
			JComboBox cb = (JComboBox) e.getSource();
			String item = (String) cb.getSelectedItem();
			AddCoachTeamPanel addCoachTeamPanel = vq.getAddCoachTeamPanel();
			if(item.equals("no team")){
				addCoachTeamPanel.setVisibleDefaultPanel();
			}else {
				addCoachTeamPanel.setVisibleAfterTeamSelectPanel();
			}
		}
    	
    }
    /*
    class addTeamsMatchAction implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			AddTeamMatchPanel addTeamMatchPanel=vq.getAddTeamMatchPanel();
			AdminActions adminActions=systemActions.getAdminActions();
			Team homeTeam= (Team) addTeamMatchPanel.getComboBoxHomeTeam().getSelectedItem();
			Team awayTeam=(Team) addTeamMatchPanel.getComboBoxAwayTeam().getSelectedItem();
			String date=addTeamMatchPanel.getTextFieldWriteDate().getText();
			int week=(Integer) addTeamMatchPanel.getComboBoxWeek().getSelectedItem();
			int inserted;
			if(homeTeam==awayTeam){
				JOptionPane.showMessageDialog(null, " *Team names must be different");
				return;
			}
			else{
				inserted=adminActions.addTeamsMatch(homeTeam, awayTeam, week, date);
			}
			if(inserted == 0){
		    	JOptionPane.showMessageDialog(null, "* cant be added");
		    	return;
		    }
		    JOptionPane.showMessageDialog(null, "added");
		    return;
		}
    	
    }
    */
    
    // add player team paneldeki add player buttonuna basıldıgında çalısır
    class addPlayerButtonAction implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			AddPlayerTeamPanel addPlayerTeamPanel = vq.getAddPlayerTeamPanel();
			String playerName  = addPlayerTeamPanel.getPlayerNameText().getText();
			String playerSurName = addPlayerTeamPanel.getPlayerSurnameText().getText();
			String playerBirthDate = addPlayerTeamPanel.getPlayerBirthDateText().getText();
		    String playerBirthPlace = addPlayerTeamPanel.getPlayerBirthPlaceText().getText();
		    String playerLicenceNumber = addPlayerTeamPanel.getPlayerLicenceNumberText().getText();
		    if(!isValidText(playerName)){
		    	JOptionPane.showMessageDialog(null, "player name should not be null");
		    	return;
		    }else if(!isValidText(playerSurName)){
		    	JOptionPane.showMessageDialog(null, "player surname shoul not be null");
		    }else if(!isValidText(playerBirthPlace)){
		    	JOptionPane.showMessageDialog(null, "player birth place  should not be null");
		    	return;
		    }else if(!isValidText(playerLicenceNumber)){
		    	JOptionPane.showMessageDialog(null, "player licence number should not be null");
		    	return;
		    }else if (!isFormattedText(addPlayerTeamPanel.getPlayerBirthDateText())){
		    	JOptionPane.showMessageDialog(null, "birth date is not valid format");
		    	return;
		    }  
		   
		    String playerTeam = null;
		    String beginLicenceDate = null;
		    String finishLicenceDate = null;
		    AdminActions adminActions = systemActions.getAdminActions();
		    int inserted = 0;
		    if(addPlayerTeamPanel.getTeamsComboBox().getSelectedIndex() != 0){
		    	beginLicenceDate =  addPlayerTeamPanel.getPlayerLicenceBeginText().getText();
		    	finishLicenceDate = addPlayerTeamPanel.getPlayerLicenceFinishText().getText();
		    	// to do  date valid
		    	playerTeam = (String) addPlayerTeamPanel.getTeamsComboBox().getSelectedItem();
		    inserted = adminActions.addPlayer(playerName, playerSurName, playerBirthDate, playerBirthPlace, Integer.parseInt(playerLicenceNumber), playerTeam, beginLicenceDate, finishLicenceDate);
		       
		    }else{
		    inserted = 	adminActions.addPlayer(playerName, playerSurName, playerBirthDate, playerBirthPlace, Integer.parseInt(playerLicenceNumber));
		    }
		    vq.getContentPane().setVisible(false);
		    vq.setContentPane(vq.getPlayerAddedVerifyPanel());
		    vq.getPlayerAddedVerifyPanel().setVisible(true);
		    if(inserted == 0){
		    	vq.getPlayerAddedVerifyPanel().getOkeyMessageLabel().setText("the player can not be added");
		    	return;
		    }
		    vq.getPlayerAddedVerifyPanel().getOkeyMessageLabel().setText("the player is added");
		    return;
		}
    }
     // search Coach 
    class searchCoachByNameAction implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			String command=arg0.getActionCommand();
			CoachSearchPanel coachSearchPanel=vq.getCoachSearchPanel();
			
			if(command.equals("byName")){
				coachSearchPanel.setVisibleSearchNamePanel();
				}
			else if(command.equals("byTeam")) {
				
				coachSearchPanel.setVisibleSearchTeamPanel();
				coachSearchPanel.getComboBox().setVisible(true);
				
			}
			}
    }
        class searchCoachResultButtonAction implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			CoachSearchPanel coachSearchPanel=vq.getCoachSearchPanel();
			List coachList=null;
			if(coachSearchPanel.getRdbtnSearchByName().isSelected()){
				//coachSearchPanel.setVisibleSearchNamePanel();	
				String name=coachSearchPanel.getSearchByNametextField().getText();
				String surname=coachSearchPanel.getSearchBySurnametextField().getText();
					if(name == null || name.trim().length() < 1){
					JOptionPane.showMessageDialog(null, " *name should not be null");
					return;
					}else if(surname == null || surname.trim().length() < 1){
					JOptionPane.showMessageDialog(null, " *surname should not be null");
					return;
					}
				CoachActions coachActions=systemActions.getCoachActions();
				coachList=coachActions.searchCoachByName(name, surname);
				coachSearchPanel.addCoachSpecialitiesToTable(coachList);
				coachSearchPanel.getSearchByNametextField().setText(""); //After pressing button...
				coachSearchPanel.getSearchBySurnametextField().setText("");
			}
			else if(coachSearchPanel.getRdbtnSearchByTeam().isSelected()){
				
				String teamName=(String) coachSearchPanel.getComboBox().getSelectedItem();
				if(teamName == null || teamName.trim().length() < 1){
					JOptionPane.showMessageDialog(null, " *team name should not be null");
					return;
					}
			
			CoachActions coachActions=systemActions.getCoachActions();
			coachList=coachActions.searchCoachByTeam(teamName);
			coachSearchPanel.addCoachSpecialitiesToTable(coachList);
			}
			
		}
    	
    }
        class addCoachAction implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			AddCoachTeamPanel addCoachTeamPanel=vq.getAddCoachTeamPanel();
			AdminActions adminActions=systemActions.getAdminActions();
			String agreementBegin=null;
			String agreementFinish=null;
			String coachTeam=null;
			int insert=0;
			
			String name=addCoachTeamPanel.getTextFieldCoachName().getText();
			String surname=addCoachTeamPanel.getTextFieldCoachSurname().getText();
			String birthdate=addCoachTeamPanel.getTextFieldCoachBirthdate().getText();
			String birthplace=addCoachTeamPanel.getTextFieldCoachBirthplace().getText();
			
			if(name==null || name.trim().length()<1){
				JOptionPane.showMessageDialog(null, " * name shouldn't be null");
				return;
			}
			else if(surname==null || surname.trim().length()<1){
				JOptionPane.showMessageDialog(null, " * surname shouldn't be null");
				return;
			}
			else if(birthdate==null || birthdate.trim().length()<1){
				JOptionPane.showMessageDialog(null, " * birthdate shouldn't be null");
				return;
			}
			else if(birthplace==null || birthplace.trim().length()<1){
				JOptionPane.showMessageDialog(null, " * birthplace shouldn't be null");
				return;
			}
			
			if(addCoachTeamPanel.getComboBox().getSelectedIndex()!=0){
				agreementBegin=addCoachTeamPanel.getTextFieldCoachAgrement().getText();
				agreementFinish=addCoachTeamPanel.getTextFieldAgrementFinish().getText();
				coachTeam=(String)addCoachTeamPanel.getComboBox().getSelectedItem();
				insert=adminActions.addCoach(name, surname, birthplace, birthdate, agreementBegin, agreementFinish);
			}
			else{
				insert=adminActions.addCoach(name, surname, birthplace, birthdate);
			}
			vq.getContentPane().setVisible(false);
			vq.setContentPane(vq.getCoachAddedVerifyPanel());
		    vq.getCoachAddedVerifyPanel().setVisible(true);
		    if(insert == 0){
		    	vq.getCoachAddedVerifyPanel().getLabelMessage().setText("the coach can not be added");
		    	return;
		    }
		    vq.getCoachAddedVerifyPanel().getLabelimage().setText("the coach is added");
		    return;

		}
    	
    }
     // search Player Paneldeki 
    class searchPlayerButtonAction implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			PlayerSearchPanel playerSearchPanel = vq.getPlayerSearchPanel();
			List playerList = null;
			if(playerSearchPanel.getSearchNameRadioButton().isSelected()){
				String name = playerSearchPanel.getSearchedPlayerNameTextField().getText();
				String surname = playerSearchPanel.getSearchedPlayerSurnameTextField_1().getText();
				if(name == null || name.trim().length() < 1){
					JOptionPane.showMessageDialog(null, "name should not be null");
					return;
				}else if(surname == null || surname.trim().length() < 1){
					JOptionPane.showMessageDialog(null, "surname should not be null");
					return;
				}
               PlayerActions playerActions = systemActions.getPlayerActions();
               playerList = playerActions.searchPlayerByName(name, surname);
               playerSearchPanel.addPlayersToPlayerTable(playerList);
			}else if(playerSearchPanel.getSearchLicenceRadioButton().isSelected()){
				String licenceNo =  playerSearchPanel.getSearchedLicenceNoTextField().getText();
				if(licenceNo == null || licenceNo.trim().length() < 1){
					JOptionPane.showMessageDialog(null, "licence no should not be null");
					return;
				}
				int lc = -1;
				try{
				  lc = Integer.parseInt(licenceNo);
				}catch(NumberFormatException ex){
					JOptionPane.showMessageDialog(null, "licence no should be number format");
					return;
				}
				PlayerActions playerActions = systemActions.getPlayerActions();
				playerList = playerActions.searchPlayerByLicenceNo(lc);
			}else if (playerSearchPanel.getSearchTeamRadioButton().isSelected()){
				String teamName = (String) playerSearchPanel.getTeamsComboBox().getSelectedItem();
		
				PlayerActions playerActions = systemActions.getPlayerActions();
				playerList = playerActions.searchPlayerByTeam(teamName);
			}
			playerSearchPanel.addPlayersToPlayerTable(playerList);
		}
    }
    
    class selectAdminButtonAction implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			MainOptionPanel mainOptionPanel=vq.getMainOptionPanel();
			
			AdminLoginPanel adminLoginPanel=vq.getAdminLoginPanel();
			
			if(mainOptionPanel.getBtnAdmin().isEnabled()){
					
				   vq.getContentPane().setVisible(false);
				   adminLoginPanel.setVisible(true);
				   vq.setContentPane(adminLoginPanel);
				   
			}
		}
    	
    }
        class selectEnterMatchResult implements ActionListener{

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				AdminOptions adminOptions=vq.getAdminOptions();
				if(adminOptions.getBtnEnterMatchResult().isEnabled()){
					JOptionPane.showMessageDialog(null, "Under Construction");
				}
			}
        	
        }
        class selectBackToAdminOptions implements ActionListener {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
			
				AddCoachTeamPanel addCoachTeamPanel=vq.getAddCoachTeamPanel();
				AddPlayerTeamPanel addPlayerTeamPanel=vq.getAddPlayerTeamPanel();
				AddTeamMatchPanel addTeamMatchPanel=vq.getAddTeamMatchPanel();
			    AddTeamPanel addTeamPanel=vq.getAddTeamPanel();
			    AdminOptions adminOptions=vq.getAdminOptions();
			    if(addCoachTeamPanel.getBtnBackToAdminOptionsSystem().isEnabled()){
			    	vq.getContentPane().setVisible(false);
			    	vq.setContentPane(adminOptions);
			    	adminOptions.setVisible(true);
			    }
			     if(addPlayerTeamPanel.getBtnBackToAdminOptionsSystem().isEnabled()){
			    	vq.getContentPane().setVisible(false);
			    	vq.setContentPane(adminOptions);
			    	adminOptions.setVisible(true);
			    }
			     if(addTeamMatchPanel.getBtnBackToAdminOptionsSystem().isEnabled()){
			    	 vq.getContentPane().setVisible(false);
				    	vq.setContentPane(adminOptions);
				    	adminOptions.setVisible(true);
			     }
			     if(addTeamPanel.getBtnBackToAdminOptionsSystem().isEnabled()){
			    	 vq.getContentPane().setVisible(false);
				    	vq.setContentPane(adminOptions);
				    	adminOptions.setVisible(true);
			     }
			}
        	
        }
        
        class selectEnterTeam implements ActionListener{

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				AdminOptions adminOptions =vq.getAdminOptions();
				//AdminOptions adminOptions2=(AdminOptions) vq.getAdminOptions().getAdminActionspanel();
				//AdminOptions adminOptions3=(AdminOptions) vq.getAdminOptions().getAdminButtonspanel();
				AddTeamPanel addTeamPanel=vq.getAddTeamPanel();
				if(adminOptions.getBtnEnterTeam().isEnabled()){
					
					vq.getContentPane().setVisible(false);
					vq.setContentPane(addTeamPanel);
					addTeamPanel.setVisible(true);
				}
			}
        	
        }
        class selectEnterMatch implements ActionListener {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				AdminOptions adminOptions =vq.getAdminOptions();
				AddTeamMatchPanel addTeamMatchPanel=vq.getAddTeamMatchPanel();
				if(addTeamMatchPanel.getBtnAddMatch().isEnabled()){
					vq.getContentPane().setVisible(false);
					vq.setContentPane(addTeamMatchPanel);
					addTeamMatchPanel.setVisible(true);
				}
			}
        	
        }
        class selectEnterPlayer implements ActionListener{

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				AdminOptions adminOptions =vq.getAdminOptions();
				AddPlayerTeamPanel addPlayerTeamPanel=vq.getAddPlayerTeamPanel();
				if(addPlayerTeamPanel.getAddPlayerButton().isEnabled()){
					vq.getContentPane().setVisible(false);
					vq.setContentPane(addPlayerTeamPanel);
					addPlayerTeamPanel.setVisible(true);
				}
			}
        	
        }
        class selectEnterCoach implements ActionListener {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				AdminOptions adminOptions =vq.getAdminOptions();
				AddCoachTeamPanel addCoachTeamPanel=vq.getAddCoachTeamPanel();
				if(addCoachTeamPanel.getBtnAddCoach().isEnabled()){
					vq.getContentPane().setVisible(false);
					vq.setContentPane(addCoachTeamPanel);
					addCoachTeamPanel.setVisible(true);
				}
			}
        	
        }

    
    // search Match panelindeki buttona basıldıgında calısır
    
    class searchMatchButtonAction implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
		  MatchSearchPanel matchSearchPanel = vq.getMatchSearchPanel();
		  List<Match> matches = null;
		  MatchActions matchActions = systemActions.getMatchActions();
		  if(matchSearchPanel.getSearchByTeamRadioButton().isSelected()){
			  String teamName = (String) matchSearchPanel.getTeamsComboBox().getSelectedItem();
			  matches = matchActions.searchMatchByTeam(teamName);
  		  }else if(matchSearchPanel.getSearchByDateRadioButton().isSelected()){
			  String date  = matchSearchPanel.getMatchDateText().getText();
			  if(date == null || date.length() < 1){
				  JOptionPane.showConfirmDialog(null , "The match date can not be null");
				  return;
			  }
			  matches = matchActions.searchMatchByDate(date);
		  }
		  matchSearchPanel.addMatchToMatchTable(matches);	
		}
      
    }
    // search Player panelindeki radio buttonlara basıldıgında 
    class searchPlayerOptionRadioButtonAction implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			String command = e.getActionCommand();
			PlayerSearchPanel playerSearchPanel = vq.getPlayerSearchPanel();
			if(command.equals("byName")){
				playerSearchPanel.setVisibleSearchNamePanel();
			}else if(command.equals("byLicenceNo")){
				playerSearchPanel.setVisibleSearchLicencePanel();
			}else if(command.equals("byTeam")){
				playerSearchPanel.setVisibleSearchTeamPanel();
			}
		}
    }
    
    // match search paneldeki radio buttonlara basıldıgında
    class searchMatchOptionRadioButtonAction implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
		  String command = e.getActionCommand();
		  MatchSearchPanel matchSearchPanel  = vq.getMatchSearchPanel();
		  if(command.equals("byTeam")){
			  matchSearchPanel.setSearchOptionLabelText("Team Select :");
			  matchSearchPanel.setVisibleTeamComboBox();
		  }else if(command.equals("byDate")){
			  matchSearchPanel.setSearchOptionLabelText("Match Date :");
			  matchSearchPanel.setVisibleMatchDateTetx();  
		  }
		}
    	
    }
    
	//match search panelindeki match result lardaki  match detailine gitmek için buttona basıldıgında
    class goMatchDetailOfMatchButtonAction implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			MatchSearchPanel matchSearchPanel = vq.getMatchSearchPanel();
			int row = matchSearchPanel.getMatchSearchScrollTable().convertRowIndexToModel(
					matchSearchPanel.getMatchSearchScrollTable().getEditingRow());
			
		   TableModel matchDefaultModel = matchSearchPanel.getMatchSearchScrollTable().getModel();
		   String homeTeam = (String) matchDefaultModel.getValueAt(row,MatchConstant.HOME_TEAM);
		   String awayTeam = (String) matchDefaultModel.getValueAt(row, MatchConstant.AWAY_TEAM); 
		   String skor = (String)matchDefaultModel.getValueAt(row, MatchConstant.MATCH_SKOR);
		   String matchDate = (String)matchDefaultModel.getValueAt(row, MatchConstant.MATCH_DATE);
		   String matchDetailId = matchDefaultModel.getValueAt(row, MatchConstant.MATCH_DETAIL).toString();
		   String[] matchesSkor = skor.split(":");
		   int matchId = (Integer)matchDefaultModel.getValueAt(row, MatchConstant.MATCH_ID);
		   MatchDetailPanel matchDetailPanel = vq.getMatchDetailPanel();
		   
		   matchDetailPanel.getHomeTeamLabel().setText(homeTeam.toUpperCase());
		   matchDetailPanel.getAwayTeamLabel().setText(awayTeam.toUpperCase());
		   MatchesDetail matchesDetail = new MatchesDetail(Integer.parseInt(matchesSkor[0]),Integer.parseInt(matchesSkor[1]));
		   matchesDetail.setMatchDetailId(Integer.parseInt(matchDetailId));
		 
		   MatchActions matchActions = systemActions.getMatchActions();
		   List<Player> homePlayer = matchActions.getHomePlayerOfMatch(matchesDetail);
		   List<Player> awayPlayer = matchActions.getAwayPlayerOfMatch(matchesDetail);
		   matchDetailPanel.updateHomeTeamPlayerList(homePlayer, matchDetailPanel.getHomeTeamFrist11ScrollList());
		   matchDetailPanel.updateHomeTeamPlayerList(awayPlayer, matchDetailPanel.getAwayTeamFirst11ScrolList());
		   List homeGoals = matchActions.getHomeGoalsPlayersOfMatch(matchesDetail);
		   List awayGoals = matchActions.getAwayGoalsPlayersOfMatch(matchesDetail);
		   matchDetailPanel.updateGoalsTable(homeGoals, matchDetailPanel.getHomeTeamGoalsTable());
		   matchDetailPanel.updateGoalsTable(awayGoals, matchDetailPanel.getAwayTeamGoalsTable());
		   vq.getContentPane().setVisible(false);
		   matchDetailPanel.setVisible(true);
		   vq.setContentPane(matchDetailPanel);
		   
		}
    	
    }
    class selectUserButtonAction implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			
			MainOptionPanel mainOptionPanel=vq.getMainOptionPanel();
			UserOptionsPanel userOptionsPanel=vq.getUserOptionsPanel();
			if(mainOptionPanel.getBtnUser().isEnabled()){
				vq.getContentPane().setVisible(false);
				userOptionsPanel.setVisible(true);
				vq.setContentPane(userOptionsPanel);
			}
		}
    	
    }
    class matchDetailBackButtonAction implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			MatchSearchPanel matchSearchPanel = vq.getMatchSearchPanel();
			vq.getContentPane().setVisible(false);
			matchSearchPanel.setVisible(true);
			vq.setContentPane(matchSearchPanel);
		}}
    
	class playerOptionButtonAction implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			PlayerSearchPanel  playerSearchPanel = vq.getPlayerSearchPanel();
			UserOptionsPanel userOptionsPanel = vq.getUserOptionsPanel();
			userOptionsPanel.setRightPanel(playerSearchPanel);
			
		}
	
	}
	// to do
	class teamOptionButtonAction implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			TeamSearchPanel teamSearchPanel=vq.getTeamSearchPanel();
			UserOptionsPanel userOptionsPanel=vq.getUserOptionsPanel();
			
			userOptionsPanel.setRightPanel(teamSearchPanel);
			
		}
	}
	//to do
	class matchOptionButtonAction implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			MatchSearchPanel matchSearchPanel=vq.getMatchSearchPanel();
			UserOptionsPanel userOptionsPanel=vq.getUserOptionsPanel();
			userOptionsPanel.setRightPanel(matchSearchPanel);
		}
	}
	//TODO  
	class coachOptionButtonAction implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			CoachSearchPanel coachSearchPanel=vq.getCoachSearchPanel();
			UserOptionsPanel userOptionsPanel=vq.getUserOptionsPanel();
			userOptionsPanel.setRightPanel(coachSearchPanel);
		}
	}
	
	class weekCombobox implements ItemListener{
		@Override
		public void itemStateChanged(ItemEvent e) {
		JComboBox cb = (JComboBox) e.getSource();
		String item = cb.getSelectedItem().toString();
		fillLeagueTable(Integer.parseInt(item));
		
		}}
	class getPlayerStatisticButtonAction implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			PlayerStatisticPanel playerStatisticPanel = vq.getPlayerStatisticPanel();
			String name = playerStatisticPanel.getPlayerName().getText();
			if(name == null || name.trim().length() < 1){
				JOptionPane.showMessageDialog(null,"the name should not be null");
				return;
			} 
			String surname = playerStatisticPanel.getPlayerSurname().getText();
			if(surname == null || surname.trim().length() < 1){
				JOptionPane.showMessageDialog(null,"the surname should not be null");
				return;
			}
		StatisticActions statisticActions = systemActions.getStatisticActions();
		List playerPlayedList = statisticActions.getMatchPlayerPlayed(name, surname);
		playerStatisticPanel.addPlayerToPlayerPlayedMatch(playerPlayedList);
		List playerGoalList = statisticActions.getMatchPlayerHaveGoal(name, surname);
		playerStatisticPanel.addPlayerToHaveGoalMatch(playerGoalList);
	}
  }
	// to do
    class systemBackButtonAction implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			
		}
    }
   class  backStatisticButton implements ActionListener{
	@Override
	public void actionPerformed(ActionEvent e) {
		vq.getContentPane().setVisible(false);
		vq.getStatisticPanel().setVisible(true);
		vq.setContentPane(vq.getStatisticPanel());
	}
  }
   
   // ana user option paneline tekrar donmek için yapılır
   class backUserOptionButton implements ActionListener{

	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		vq.getContentPane().setVisible(false);
		vq.getUserOptionsPanel().setVisible(true);
		vq.setContentPane(vq.getUserOptionsPanel());
	}
   }
	 class enterPlayerStatisticButton implements  ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			 fillMostGoalScorer();
			 vq.getContentPane().setVisible(false);
			 vq.getPlayerStatisticPanel().setVisible(true);
			 vq.setContentPane(vq.getPlayerStatisticPanel());
			
		   } 
		 }
	 
    class weekComboboxOfTeamStatistic implements ItemListener {
		@Override
		public void itemStateChanged(ItemEvent e) {
			JComboBox cb = (JComboBox) e.getSource();
			String item = cb.getSelectedItem().toString();
			fillTeamStatisticTables(Integer.parseInt(item));
		}
	
    }
    
    
    public boolean isValidText(String text){
    	if(text == null || text.trim().length() < 1){
    		return false;
    	}
    	return true;
    }
    public boolean isFormattedText(JFormattedTextField text){
    	if(!text.isValid()){
    		return false;
    	}
    	return true;
    }
    class addAdminCreateButtonAction implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			AdminCreatePanel adminCreatePanel=vq.getAdminCreatePanel();
			String name=adminCreatePanel.getTextFieldAdminName().getText();
			String password=adminCreatePanel.getTextFieldAdminPassword().getText();
			String nickname=adminCreatePanel.getTextFieldNickname().getText();
			String surname=adminCreatePanel.getTextFieldAdminSurname().getText();
			String birthdate=adminCreatePanel.getTextFieldBirthdate().getText();
			String birthplace = adminCreatePanel.getTextFieldBirthplace().getText();
			AdminActions actions=systemActions.getAdminActions();
			
			int res=actions.create(Integer.parseInt(password), nickname,name,surname,birthdate,birthplace);
					if(res>0){
						JOptionPane.showMessageDialog(null, "Admin created succesfully");
					}
					else{
						JOptionPane.showMessageDialog(null, "Can't be created");
					}
			}
		}
    	
    
    class addAdminLoginButtonAction implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
		AdminLoginPanel adminLoginPanel = vq.getAdminLoginPanel();
		AdminOptions adminOptions=vq.getAdminOptions();
		String name = adminLoginPanel.getTextField().getText();
		@SuppressWarnings("deprecation")
		String password = adminLoginPanel.getPasswordField().getText();
		AdminActions  adminActions = systemActions.getAdminActions();
		if(name==null || name.trim().length()<1 ){
			JOptionPane.showMessageDialog(null, " *name shouldn't be null");
			return;
		}
		else if(password==null || password.trim().length()<1){
			JOptionPane.showMessageDialog(null, " *password shouldn't be null");
			return;
		}
		boolean valid = adminActions.login(Integer.parseInt(password), name);
		
		if(valid){
			JOptionPane.showMessageDialog(null,"You have logged in.");
			vq.getContentPane().setVisible(false);
			adminOptions.setVisible(true);
			vq.setContentPane(adminOptions);
			
		}else{
			JOptionPane.showMessageDialog(null,"Invalid information.Try again");
		}
		
			
		}
    	
    }
    public void fillAddPlayerPanelComboBox(){
          TeamActions teamAction = systemActions.getTeamActions();
          List teamList = teamAction.getTeams();
          //List week=null;
          vq.getAddPlayerTeamPanel().fillComboBoxWithTeams(teamList);
          vq.getPlayerSearchPanel().fillComboBoxWithTeams(teamList);
          vq.getMatchSearchPanel().fillTeamComboBox(teamList);
          vq.getAddCoachTeamPanel().fillComboBoxWithTeams(teamList);
          vq.getCoachSearchPanel().fillComboBoxWithTeams(teamList);
          vq.getAddTeamMatchPanel().fillComboBoxWithTeams(teamList);
          vq.getAddTeamMatchPanel().fillComboBoxWithAwayTeams(teamList);
          //vq.getAddTeamMatchPanel().fillComboBoxWithWeeks(week);
     }
    
     public void fillLeagueTable(int week){
    	  StatisticActions statisticActions = systemActions.getStatisticActions();
    	  List leagueMapList = statisticActions.getLeaguTableOfWeek(week);
    	  StatisticPanel statisticPanel = vq.getStatisticPanel();
    	  statisticPanel.addLeagueTable(leagueMapList);
     }
     public void fillMostGoalScorer(){
    	 StatisticActions statisticActions =  systemActions.getStatisticActions();
    	 List mostScorerGoal = statisticActions.getMostScorerPlayers();
    	 PlayerStatisticPanel playerStatisticPanel = vq.getPlayerStatisticPanel();
    	 playerStatisticPanel.addGoalToMostGoalScorerTable(mostScorerGoal);
     }
     public void fillTeamStatisticTables(int week){
    	 StatisticActions statisticActions = systemActions.getStatisticActions();
    	 List winList = statisticActions.winNumberOfTeams(week);
    	 List lostList = statisticActions.lostNumberOfTeams(week);
    	 List mostGoalList = statisticActions.mostGoalTeam(week);
    	 List mostGoalAgainst = statisticActions.mostGoalAgainstTeam(week);
    	 TeamStatisticPanel teamStatisticPanel = vq.getTeamStatisticPanel();
    	 teamStatisticPanel.addWinNumberOfTeams(winList);
    	 teamStatisticPanel.addLostNumberOfTeams(lostList);
    	 teamStatisticPanel.addMostGoalsOfTeams(mostGoalList);
    	 teamStatisticPanel.addMostGoalAgainstOfTeams(mostGoalAgainst);
    	 
     }
}


